<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of demrbf1</title>
  <meta name="keywords" content="demrbf1">
  <meta name="description" content="DEMRBF1 Demonstrate simple regression using a radial basis function network.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; demrbf1.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>demrbf1
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>DEMRBF1 Demonstrate simple regression using a radial basis function network.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>This is a script file. </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">DEMRBF1 Demonstrate simple regression using a radial basis function network.

    Description
    The problem consists of one input variable X and one target variable
    T with data generated by sampling X at equal intervals and then
    generating target data by computing SIN(2*PI*X) and adding Gaussian
    noise. This data is the same as that used in demmlp1.

    Three different RBF networks (with different activation functions)
    are trained in two stages. First, a Gaussian mixture model is trained
    using the EM algorithm, and the centres of this model are used to set
    the centres of the RBF.  Second, the output weights (and biases) are
    determined using the pseudo-inverse of the design matrix.

    See also
    <a href="demmlp1.html" class="code" title="">DEMMLP1</a>, <a href="rbf.html" class="code" title="function net = rbf(nin, nhidden, nout, rbfunc, outfunc, prior, beta)">RBF</a>, <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">RBFFWD</a>, <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">GMM</a>, <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">GMMEM</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="rbf.html" class="code" title="function net = rbf(nin, nhidden, nout, rbfunc, outfunc, prior, beta)">rbf</a>	RBF	Creates an RBF network with specified architecture</li><li><a href="rbferr.html" class="code" title="function [e, edata, eprior] = rbferr(net, x, t)">rbferr</a>	RBFERR	Evaluate error function for RBF network.</li><li><a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>	RBFFWD	Forward propagation through RBF network with linear outputs.</li><li><a href="rbftrain.html" class="code" title="function [net, options] = rbftrain(net, options, x, t)">rbftrain</a>	RBFTRAIN Two stage training of RBF network.</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="demnlab.html" class="code" title="function demnlab(action);">demnlab</a>	DEMNLAB A front-end Graphical User Interface to the demos</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <span class="comment">%DEMRBF1 Demonstrate simple regression using a radial basis function network.</span>
0002 <span class="comment">%</span>
0003 <span class="comment">%    Description</span>
0004 <span class="comment">%    The problem consists of one input variable X and one target variable</span>
0005 <span class="comment">%    T with data generated by sampling X at equal intervals and then</span>
0006 <span class="comment">%    generating target data by computing SIN(2*PI*X) and adding Gaussian</span>
0007 <span class="comment">%    noise. This data is the same as that used in demmlp1.</span>
0008 <span class="comment">%</span>
0009 <span class="comment">%    Three different RBF networks (with different activation functions)</span>
0010 <span class="comment">%    are trained in two stages. First, a Gaussian mixture model is trained</span>
0011 <span class="comment">%    using the EM algorithm, and the centres of this model are used to set</span>
0012 <span class="comment">%    the centres of the RBF.  Second, the output weights (and biases) are</span>
0013 <span class="comment">%    determined using the pseudo-inverse of the design matrix.</span>
0014 <span class="comment">%</span>
0015 <span class="comment">%    See also</span>
0016 <span class="comment">%    DEMMLP1, RBF, RBFFWD, GMM, GMMEM</span>
0017 <span class="comment">%</span>
0018 
0019 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0020 
0021 
0022 <span class="comment">% Generate the matrix of inputs x and targets t.</span>
0023 randn(<span class="string">'state'</span>, 42);
0024 rand(<span class="string">'state'</span>, 42);
0025 ndata = 20;            <span class="comment">% Number of data points.</span>
0026 noise = 0.2;            <span class="comment">% Standard deviation of noise distribution.</span>
0027 x = (linspace(0, 1, ndata))';
0028 t = sin(2*pi*x) + noise*randn(ndata, 1);
0029 mu = mean(x);
0030 sigma = std(x);
0031 tr_in = (x - mu)./(sigma);
0032 
0033 clc
0034 disp(<span class="string">'This demonstration illustrates the use of a Radial Basis Function'</span>)
0035 disp(<span class="string">'network for regression problems.  The data is generated from a noisy'</span>)
0036 disp(<span class="string">'sine function.'</span>)
0037 disp(<span class="string">' '</span>)
0038 disp(<span class="string">'Press any key to continue.'</span>)
0039 pause
0040 <span class="comment">% Set up network parameters.</span>
0041 nin = 1;            <span class="comment">% Number of inputs.</span>
0042 nhidden = 7;            <span class="comment">% Number of hidden units.</span>
0043 nout = 1;            <span class="comment">% Number of outputs.</span>
0044 
0045 clc
0046 disp(<span class="string">'We assess the effect of three different activation functions.'</span>)
0047 disp(<span class="string">'First we create a network with Gaussian activations.'</span>)
0048 disp(<span class="string">' '</span>)
0049 disp(<span class="string">'Press any key to continue.'</span>)
0050 pause
0051 <span class="comment">% Create and initialize network weight and parameter vectors.</span>
0052 net = <a href="rbf.html" class="code" title="function net = rbf(nin, nhidden, nout, rbfunc, outfunc, prior, beta)">rbf</a>(nin, nhidden, nout, <span class="string">'gaussian'</span>);
0053 
0054 disp(<span class="string">'A two-stage training algorithm is used: it uses a small number of'</span>)
0055 disp(<span class="string">'iterations of EM to position the centres, and then the pseudo-inverse'</span>)
0056 disp(<span class="string">'of the design matrix to find the second layer weights.'</span>)
0057 disp(<span class="string">' '</span>)
0058 disp(<span class="string">'Press any key to continue.'</span>)
0059 pause
0060 disp(<span class="string">'Error values from EM training.'</span>)
0061 <span class="comment">% Use fast training method</span>
0062 options = foptions;
0063 options(1) = 1;        <span class="comment">% Display EM training</span>
0064 options(14) = 10;    <span class="comment">% number of iterations of EM</span>
0065 net = <a href="rbftrain.html" class="code" title="function [net, options] = rbftrain(net, options, x, t)">rbftrain</a>(net, options, tr_in, t);
0066 
0067 disp(<span class="string">' '</span>)
0068 disp(<span class="string">'Press any key to continue.'</span>)
0069 pause
0070 clc
0071 disp(<span class="string">'The second RBF network has thin plate spline activations.'</span>)
0072 disp(<span class="string">'The same centres are used again, so we just need to calculate'</span>)
0073 disp(<span class="string">'the second layer weights.'</span>)
0074 disp(<span class="string">' '</span>)
0075 disp(<span class="string">'Press any key to continue.'</span>)
0076 pause
0077 <span class="comment">% Create a second RBF with thin plate spline functions</span>
0078 net2 = <a href="rbf.html" class="code" title="function net = rbf(nin, nhidden, nout, rbfunc, outfunc, prior, beta)">rbf</a>(nin, nhidden, nout, <span class="string">'tps'</span>);
0079 
0080 <span class="comment">% Re-use previous centres rather than calling rbftrain again</span>
0081 net2.c = net.c;
0082 [y, act2] = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net2, tr_in);
0083 
0084 <span class="comment">% Solve for new output weights and biases from RBF activations</span>
0085 temp = pinv([act2 ones(ndata, 1)]) * t;
0086 net2.w2 = temp(1:nhidden, :);
0087 net2.b2 = temp(nhidden+1, :);
0088 
0089 disp(<span class="string">'The third RBF network has r^4 log r activations.'</span>)
0090 disp(<span class="string">' '</span>)
0091 disp(<span class="string">'Press any key to continue.'</span>)
0092 pause
0093 <span class="comment">% Create a third RBF with r^4 log r functions</span>
0094 net3 = <a href="rbf.html" class="code" title="function net = rbf(nin, nhidden, nout, rbfunc, outfunc, prior, beta)">rbf</a>(nin, nhidden, nout, <span class="string">'r4logr'</span>);
0095 
0096 <span class="comment">% Overwrite weight vector with parameters from first RBF</span>
0097 net3.c = net.c;
0098 [y, act3] = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net3, tr_in);
0099 temp = pinv([act3 ones(ndata, 1)]) * t;
0100 net3.w2 = temp(1:nhidden, :);
0101 net3.b2 = temp(nhidden+1, :);
0102 
0103 disp(<span class="string">'Now we plot the data, underlying function, and network outputs'</span>)
0104 disp(<span class="string">'on a single graph to compare the results.'</span>)
0105 disp(<span class="string">' '</span>)
0106 disp(<span class="string">'Press any key to continue.'</span>)
0107 pause
0108 <span class="comment">% Plot the data, the original function, and the trained network functions.</span>
0109 plotvals = [x(1):0.01:x(end)]';
0110 inputvals = (plotvals-mu)./sigma;
0111 y = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net, inputvals);
0112 y2 = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net2, inputvals);
0113 y3 = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net3, inputvals);
0114 fh1 = figure;
0115 
0116 plot(x, t, <span class="string">'ob'</span>)
0117 hold on
0118 xlabel(<span class="string">'Input'</span>)
0119 ylabel(<span class="string">'Target'</span>)
0120 axis([x(1) x(end) -1.5 1.5])
0121 [fx, fy] = fplot(<span class="string">'sin(2*pi*x)'</span>, [x(1) x(end)]);
0122 plot(fx, fy, <span class="string">'-r'</span>, <span class="string">'LineWidth'</span>, 2)
0123 plot(plotvals, y, <span class="string">'--g'</span>, <span class="string">'LineWidth'</span>, 2)
0124 plot(plotvals, y2, <span class="string">'k--'</span>, <span class="string">'LineWidth'</span>, 2)
0125 plot(plotvals, y3, <span class="string">'-.c'</span>, <span class="string">'LineWidth'</span>, 2)
0126 legend(<span class="string">'data'</span>, <span class="string">'function'</span>, <span class="string">'Gaussian RBF'</span>, <span class="string">'Thin plate spline RBF'</span>, <span class="keyword">...</span>
0127   <span class="string">'r^4 log r RBF'</span>);
0128 hold off
0129 
0130 disp(<span class="string">'RBF training errors are'</span>);
0131 disp([<span class="string">'Gaussian '</span>, num2str(<a href="rbferr.html" class="code" title="function [e, edata, eprior] = rbferr(net, x, t)">rbferr</a>(net, tr_in, t)), <span class="string">' TPS '</span>,  <span class="keyword">...</span>
0132 num2str(<a href="rbferr.html" class="code" title="function [e, edata, eprior] = rbferr(net, x, t)">rbferr</a>(net2, tr_in, t)), <span class="string">' R4logr '</span>, num2str(<a href="rbferr.html" class="code" title="function [e, edata, eprior] = rbferr(net, x, t)">rbferr</a>(net3, tr_in, t))]);
0133 
0134 disp(<span class="string">' '</span>)
0135 disp(<span class="string">'Press any key to end.'</span>)
0136 pause
0137 close(fh1);
0138 clear all;</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>